package org.neogenesis.web.datamodel;

import java.util.Comparator;

import org.neogenesis.web.model.Experiment;
import org.primefaces.model.SortOrder;

public class LazySorter implements Comparator<Experiment> {

	private String sortField;

	private SortOrder sortOrder;

	public LazySorter(String sortField, SortOrder sortOrder) {
		this.sortField = sortField;
		this.sortOrder = sortOrder;
	}

	@Override
	public int compare(Experiment e1, Experiment e2) {

		try {
			Object value1 = Experiment.class.getField(this.sortField).get(e1);
			Object value2 = Experiment.class.getField(this.sortField).get(e2);
			
			int value = ((Comparable) value1).compareTo(value2);

			return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
